﻿namespace SDT.CrawlSystem.Servers.Filters
{
    /// <summary>
    /// Written by TungNX
    /// </summary>
    public sealed class UrlFilter : IUrlFilter
    {
        public UrlFilter(int minLen, string startWith, string endWith, string contain)
        {
            MinLen = minLen;
            StartWith = startWith;
            EndWith = endWith;
            Contain = contain;
        }

        #region Fields Members
        private int minLen = 0;
        public int MinLen
        {
            get { return minLen; }
            set { minLen = value; }
        }

        private string startWith = "";
        public string StartWith
        {
            get { return startWith; }
            set { startWith = value != null ? value.Trim() : ""; }
        }

        private string endWith = "";
        public string EndWith
        {
            get { return endWith; }
            set { endWith = value != null ? value.Trim() : ""; }
        }

        private string contain = "";
        public string Contain
        {
            get { return contain; }
            set { contain = value != null ? value.Trim() : ""; }
        }
        #endregion

        /// <summary>
        /// Modified Date: 16/09/2010
        /// Description : 
        /// + tolower url va contain
        /// + With url =""../../../ then we remove
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public bool AcceptUrl(string url)
        {
            url = url.ToLower();           
            
            if (minLen > 0 && url.Length < minLen)
                return false;

            if (startWith.Length > 0 && !url.StartsWith(startWith))
                return false;

            if (endWith.Length > 0 && !url.EndsWith(endWith))
                return false;

            if (contain.Length > 0 && !url.Contains(contain.ToLower()))
                return false;

            return true;
        }
    }
}